/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "InputOpTranslator.h"

namespace PQBuilder
{
	InputOpTranslator::InputOpTranslator()
	{
		_shortcutMgr=new ShortcutManager();
	}

	InputOpTranslator::~InputOpTranslator()
	{
		delete _shortcutMgr;
	}

	OpType InputOpTranslator::getOp(ShortcutType type)
	{
		return getOp(_shortcutMgr->getShortcuts(type));
	}

	OpType InputOpTranslator::getOp(ShortcutList& list)
	{
		OpType op=OP_NONE;
		int size=list.size();

		//Because shortcut already shorted by order so just traverse it
		for(int i=0;i<size;i++){
			Shortcut *shortcut=list[i];
			bool selected=true;
			int cnt=shortcut->Input.size();
			for(int j=0;j<cnt;j++){
				if(!_inputPressed[shortcut->Input[j]]){
					selected=false;
					break;
				}
			}

			if(selected){
				op=shortcut->Op;
				return op;
			}
		}

		return OP_NONE;
	}

	void InputOpTranslator::ResetInput()
	{
		for(int i=0;i<KEY_CODE_COUNT+MOUSE_BUTTON_COUNT;i++){
			_inputPressed[i]=false;
		}
	}

	void InputOpTranslator::OnKeyDown(int key)
	{
		_inputPressed[key]=true;
	}

	void InputOpTranslator::OnKeyUp(int key)
	{
		_inputPressed[key]=false;
	}
	
	void InputOpTranslator::OnMouseLeftDown(int x,int y)
	{
		_inputPressed[MOUSE_BUTTON_LEFT]=true;
	}

	void InputOpTranslator::OnMouseLeftUp(int x,int y)
	{
		_inputPressed[MOUSE_BUTTON_LEFT]=false;
	}

	void InputOpTranslator::OnMouseRightDown(int x,int y)
	{
		_inputPressed[MOUSE_BUTTON_RIGHT]=true;
	}

	void InputOpTranslator::OnMouseRightUp(int x,int y)
	{
		_inputPressed[MOUSE_BUTTON_RIGHT]=false;
	}

	void InputOpTranslator::OnMouseMidDown(int x,int y)
	{
		_inputPressed[MOUSE_BUTTON_MID]=true;
	}

	void InputOpTranslator::OnMouseMidUp(int x,int y)
	{
		_inputPressed[MOUSE_BUTTON_MID]=false;
	}

	void InputOpTranslator::OnMouseMove(int x,int y)
	{
	}

}
